/**
 * Copyright (C) 2012  The CFuture Android SDK Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.android.toolkit.util;
import java.lang.reflect.Array;

public class ArrayUtils {
	
	/**
	 * 根据数组类型的class创建对应类型的数组
	 * @param <T> 目标类型
	 * @param clazz
	 * @param length 数组长度
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T[] newArrayByArrayClass(Class<T[]> clazz, int length) {
		return (T[]) Array.newInstance(clazz.getComponentType(), length);
	}
	
	/**
	 * 根据普通类型的class创建数组
	 * @param <T> 目标类型
	 * @param clazz
	 * @param length 数组长度
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T[] newArrayByClass(Class<T> clazz, int length) {
		return (T[]) Array.newInstance(clazz, length);
	}

	/*public static void main(String[] args) {
		// 判断一个Class是否是数组类型，可以用Class实例的isArray方法。
		String[] byArray = newArrayByArrayClass(String[].class, 10);
		String[] byOne = newArrayByClass(String.class, 10);
	}*/

}